﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		ReferrerRepository.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 //   File:            ReferrerRepository.cs
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System;
using System.Collections.Generic;
using System.Linq;
using Dexter.Core.Data.Repository;
using Dexter.Domain.Model;
using Dexter.Domain.Results;
using Dexter.Logger.Contracts;
using Dexter.Repository.NHibernate.Queries;
using NHibernate;
using Topics.Radical.Validation;
using ISessionFactory = Dexter.Repository.NHibernate.SessionFactory.ISessionFactory;

namespace Dexter.Repository.NHibernate.Repositories {
	
	/// <summary>
	/// The implementation of <see cref="IReferrerRepository"/>.
	/// </summary>
	/// <example>The following is an example of initializing 
	///          an <c>ReferrerRepository</c> type:
	///   <code>
	///       // Create the type.
	///       ReferrerRepository ds = new ReferrerRepository;
	/// 
	///       if ( null == ds )
	///           return;
	/// 
	///       return ds.DoSomething( 5 );
	///   </code>
	/// </example>
	public class ReferrerRepository : RepositoryBase <Referrer> , IReferrerRepository {
		
		/// <summary>
		/// Initializes a new instance of the <see cref="ReferrerRepository"/> class.
		/// </summary>
		/// <param name="sessionFactory">The session factory.</param>
		/// <param name="logger">The logger.</param>
		public ReferrerRepository ( ISessionFactory sessionFactory , ILogger logger )
			: base ( sessionFactory , logger ) {}

		#region Implementation of IReferrerRepository

		/// <summary>
		/// Adds the specified item.
		/// </summary>
		/// <param name="item">The item.</param>
		public override void Add ( Referrer item ) {
			this.Save ( item );
		}

		/// <summary>
		/// Adds the specified referrer to the repository.
		/// </summary>
		/// <param name="item">The item to save.</param>
		/// <exception cref="ArgumentNullException"> If <paramref name="item"/> is <c>null</c>.</exception>
		public virtual void Save ( Referrer item ) {
			Ensure.That ( item )
				.Named ( "item" )
				.WithPreview ( ( v , e ) => Logger.Warn ( v.GetFullErrorMessage ( ) , e ) )
				.IsNotNull ( )
				.WithMessage ( "Invalid referrer" );

			item.ValidateForSave ( );

			Session.Save ( item );
		}

		/// <summary>
		/// Gets the referrers.
		/// </summary>
		/// <param name="pageSize">The number of items to retrieve.</param>
		/// <param name="pageIndex">Index of the page (0 based).</param>
		/// <returns>
		/// An concrete implementation of <see cref="IPagedResult{T}"/>. The result will never be null.
		/// </returns>
		/// <exception cref="ArgumentOutOfRangeException"> If <paramref name="pageIndex"/> is lesser than 0.</exception>
		/// <exception cref="ArgumentOutOfRangeException"> If <paramref name="pageSize"/> is lesser than 1.</exception>
		public virtual IPagedResult<Referrer> GetReferrers ( int pageIndex , int pageSize ) {
			return GetReferrers ( pageIndex , pageSize , true );
		}

		/// <summary>
		/// Gets the referrers.
		/// </summary>
		/// <param name="pageSize">The number of items to retrieve.</param>
		/// <param name="pageIndex">Index of the page (0 based).</param>
		/// <param name="enableTracking">if set to <c>true</c> the tracking system of the return object will be enable.</param>
		/// <returns>
		/// An concrete implementation of <see cref="IPagedResult{T}"/>. The result will never be null.
		/// </returns>
		/// <exception cref="ArgumentOutOfRangeException"> If <paramref name="pageIndex"/> is lesser than 0.</exception>
		/// <exception cref="ArgumentOutOfRangeException"> If <paramref name="pageSize"/> is lesser than 1.</exception>
		public virtual IPagedResult<Referrer> GetReferrers ( int pageIndex , int pageSize , bool enableTracking ) {
			Ensure.That ( pageIndex )
				.Named ( "pageIndex" )
				.WithPreview ( ( v , e ) => Logger.Warn ( v.GetFullErrorMessage ( ) , e ) )
				.IsGreaterThen ( 0 , Or.Equal )
				.WithMessage ( "Invalid page index." );

			Ensure.That ( pageSize )
				.Named ( "pageSize" )
				.WithPreview ( ( v , e ) => Logger.Warn ( v.GetFullErrorMessage ( ) , e ) )
				.IsGreaterThen ( 0 )
				.WithMessage ( "Invalid page size." );

			IFutureValue <long> total = Session.GetNamedQuery ( KnownQueries.ReferrerRepository.GetReferrersCount )
				.FutureValue <long> ( );

			IEnumerable <Referrer> result = Session.GetNamedQuery ( KnownQueries.ReferrerRepository.GetReferrers )
				.SetFirstResult ( pageIndex * pageSize )
				.SetMaxResults ( pageSize )
				.SetReadOnly ( !enableTracking )
				.Future <Referrer> ( );

			return new PagedResult<Referrer>(pageIndex, pageSize, result.ToList(), total.Value);
		}

		#endregion
	}
}
